package com.foreveross.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.foreveross.common.CommonUtil;
import com.foreveross.common.Constants;
import com.foreveross.service.LogService;
import com.foreveross.service.SysUserService;
import com.foreveross.util.AuthUtil;
import com.foreveross.vo.SysUser;
import com.foreveross.vo.common.ResultVo;

@Controller
@RequestMapping(value = "/sysLoginController")
public class SysLoginController  extends BaseCostroller {
	private Logger log = Logger.getLogger(SysLoginController.class);

	@Resource
	private SysUserService userService;

	@Resource
	private LogService logService;
	
	/**
	 * 退出登陆
	 * @param request
	 * @param response
	 * @param userId
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/outLogin")
	public void outLogin(HttpServletRequest request,
			HttpServletResponse response, String userId) {
		ResultVo resultVo = new ResultVo();
		Boolean outState=false;
		try {
			SysUser sysUser = (SysUser) request.getSession().getAttribute(
					userId);
			if (sysUser != null) {
				request.getSession().removeAttribute(userId);
				outState=true;
			}
		} catch (Exception e) {
		}
		resultVo.setSuccess(outState);
		if(!outState)
		resultVo.setMessage("退出登陆异常");
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
	
	/**
	 * 修改用户密码
	 * @param request
	 * @param response
	 * @param userId
	 * @param oldPassword
	 * @param newPassword
	 * @param affirmNewPassword
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/updateUserPass")
	public void updateUserPass(HttpServletRequest request,
			HttpServletResponse response, String userId, String oldPassword,String newPassword,String affirmNewPassword) {
		ResultVo resultVo = new ResultVo();
		Boolean updateState=false;
		if (userId != null && !"".equals(userId) && oldPassword != null
				&& !"".equals(oldPassword) && newPassword != null
						&& !"".equals(newPassword)&& affirmNewPassword != null
								&& !"".equals(affirmNewPassword)) {
			try {
				if(newPassword.equals(affirmNewPassword))
				{
					int mimaLength=newPassword.length();
					if(mimaLength>=6&&mimaLength<=16)
					{   
						SysUser sysUser =(SysUser)request.getSession().getAttribute(userId);
						if(sysUser!=null)
						{
							if(sysUser.getPassword().equals(oldPassword))
							{
								 if(userService.updateUserPass(sysUser.getId(), newPassword))
								 {
									 updateState=true;
								 }
								 else
								 {
									 resultVo.setMessage("当前用户异常或者更新密码失败！");
								 }
							}
							else
							{
								resultVo.setMessage("输入旧密码不对！");
							}
						}
						else
						{
							resultVo.setMessage("当前用户还没登陆不能修改密码！");
						}
					}
					else
					{
						resultVo.setMessage("新密码长度必须在6到16之间！");	
					}
				}
				else
				{
					resultVo.setMessage("新密码和确认新密码不一致！");
				}
			} catch (Exception e) {
				e.printStackTrace();
				resultVo.setMessage("更新密码失败！");
			}
		}
		resultVo.setSuccess(updateState);
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}

	
	/**
	 * 用户登录
	 * @param request
	 * @param response
	 * @param account
	 * @param password
	 */
	@RequestMapping(method = RequestMethod.POST, value = "/login")
	public void login(HttpServletRequest request, HttpServletResponse response,
			String account, String password) {
		ResultVo resultVo = new ResultVo();
		boolean state = false;
		try {
			if (account != null && !"".equals(account) && password != null
					&& !"".equals(password)) {
			    int passwordLength =	password.length();
				if(passwordLength>=6&&passwordLength<=16)
				{
					SysUser sysUser = userService.login(account);
					if (sysUser != null) {
						if (sysUser.getPassword().equals(password)) {
							request.getSession().setAttribute(sysUser.getId(),
									sysUser);
							state = true;
							resultVo.setSuccess(true);
							resultVo.setData(sysUser);
							logService.saveLog(this.getUserIP(request),Constants.OPERATE_TYPE_BACKGROUND,Constants.LOG_LOGIN, "用户登录成功",
									sysUser.getId(), "用户ID[" + sysUser.getId()
											+ "]登录成功!");
							log.info("用户ID[" + sysUser.getId() + "]登录成功!");
						} else {
							resultVo.setMessage("登陆名和密码不匹配！");
						}
					} else {
						resultVo.setMessage("登陆名不存在！");
					}
				}
				else{
					resultVo.setMessage("密码长度必须在6到16之间！");
				}
			} else {
				resultVo.setMessage("登陆名或密码不能为空！");
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultVo.setMessage("登陆名不存在或者登陆异常！");
		}
		if (!state)
			resultVo.setSuccess(false);
		CommonUtil.PrintWriterResultVoWithJSON(response, resultVo);
	}
}
